package com.ruoyi.courses.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.courses.domain.CourseInfo;
import com.ruoyi.courses.mapper.CourseNatureMapper;
import com.ruoyi.courses.domain.CourseNature;
import com.ruoyi.courses.service.ICourseNatureService;

/**
 * 课程性质Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class CourseNatureServiceImpl implements ICourseNatureService 
{
    @Autowired
    private CourseNatureMapper courseNatureMapper;

    /**
     * 查询课程性质
     * 
     * @param natureId 课程性质主键
     * @return 课程性质
     */
    @Override
    public CourseNature selectCourseNatureByNatureId(Long natureId)
    {
        return courseNatureMapper.selectCourseNatureByNatureId(natureId);
    }

    /**
     * 查询课程性质列表
     * 
     * @param courseNature 课程性质
     * @return 课程性质
     */
    @Override
    public List<CourseNature> selectCourseNatureList(CourseNature courseNature)
    {
        return courseNatureMapper.selectCourseNatureList(courseNature);
    }

    /**
     * 新增课程性质
     * 
     * @param courseNature 课程性质
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCourseNature(CourseNature courseNature)
    {
        int rows = courseNatureMapper.insertCourseNature(courseNature);
        insertCourseInfo(courseNature);
        return rows;
    }

    /**
     * 修改课程性质
     * 
     * @param courseNature 课程性质
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCourseNature(CourseNature courseNature)
    {
        courseNatureMapper.deleteCourseInfoByCourseId(courseNature.getNatureId());
        insertCourseInfo(courseNature);
        return courseNatureMapper.updateCourseNature(courseNature);
    }

    /**
     * 批量删除课程性质
     * 
     * @param natureIds 需要删除的课程性质主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCourseNatureByNatureIds(Long[] natureIds)
    {
        courseNatureMapper.deleteCourseInfoByCourseIds(natureIds);
        return courseNatureMapper.deleteCourseNatureByNatureIds(natureIds);
    }

    /**
     * 删除课程性质信息
     * 
     * @param natureId 课程性质主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCourseNatureByNatureId(Long natureId)
    {
        courseNatureMapper.deleteCourseInfoByCourseId(natureId);
        return courseNatureMapper.deleteCourseNatureByNatureId(natureId);
    }

    /**
     * 新增课程信息信息
     * 
     * @param courseNature 课程性质对象
     */
    public void insertCourseInfo(CourseNature courseNature)
    {
        List<CourseInfo> courseInfoList = courseNature.getCourseInfoList();
        Long natureId = courseNature.getNatureId();
        if (StringUtils.isNotNull(courseInfoList))
        {
            List<CourseInfo> list = new ArrayList<CourseInfo>();
            for (CourseInfo courseInfo : courseInfoList)
            {
                courseInfo.setCourseId(natureId);
                list.add(courseInfo);
            }
            if (list.size() > 0)
            {
                courseNatureMapper.batchCourseInfo(list);
            }
        }
    }
}
